<?php

namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use App\Models\Article;

use Illuminate\Http\Request;
use App\Http\Controllers\Admin\BaseController;
use App\Exports\ArticleExport;
use Maatwebsite\Excel\Facades\Excel;
class ArticleController extends BaseController
{
    /**
     * dataTable 服务端展示 搜索 结果返回
     *
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {

        /*
         * dataTables 服务端
         * 先走到前端页面 发送ajax请求 然后返回到前端页面
         */
        if ($request->header('X-Requested-With') == "XMLHttpRequest") {


            $draw = $request->get('draw');
            $start = $request->get('start');
            $length = $request->get('length');
            $search = $request->get('search')['value'];

            $data = Article::when($search,function ($query) use ($search){

                $query->where('title','like',"%$search%");
            })->offset($start)
                ->limit($length)
                ->get()->toArray();

            //所有数据条数
            if (empty($search)){
                //如果没有所有内容 获取数据库所有条数
                $total=Article::count();

            }else{
                //如果 有搜索内容，则查询条数
                $total=Article::where('title','like',"%$search%")->count();

            }

            return [
                'draw'=>$draw,
                'recordsTotal'=>$total,
                'recordsFiltered'=>$total,  //过滤后的总条数
                'data'=>$data,
                'error'=>0

            ];

        }

        return view('admin.article.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

        return  view('admin.article.add');
    }

    /**v
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $params=$request->except(['_token','file']);

        //把地址转化为经纬度  //对接第三方 插件 高德
        $url=sprintf(config('geo.url'),config('geo.key')).$params['address'];
        $res =file_get_contents($url,true);

        $res =json_decode($res,true)['geocodes'][0]['location'];

        $res=explode(',',$res);  //dd($res);
        list($params['longitude'],$params['latitude'])=$res;
        unset($params['address']);
        $res= Article::create($params);
        if ($res){

            return redirect(route('admin.article.index'));
        }else{

            return redirect(route('admin.article.create'))->withErrors("文章添加失败");
        }
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        dd($id);
    }

    /**
     * 图片本地异步上传
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function localUploadFile(Request $request){
        //接收图片 // 平且 上传到本地磁盘
        $file = $request->file('file')->store('','article');
        $file='/uploads/article/'.$file;
        return $this->writeJson('0',['file'=>$file]);

    }

    /**
     * 表格导出
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
     */
    public function export()
    {
        return Excel::download(new ArticleExport(), 'articles.xlsx');
    }

}
